package cn.ltx.crm.settings.web.controller;

import cn.ltx.crm.Commons.contants.Constants;
import cn.ltx.crm.Commons.domin.ReturnResult;
import cn.ltx.crm.Commons.ustils.DateUtils;
import cn.ltx.crm.settings.pojo.User;
import cn.ltx.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author wataxi
 * @Date 2023/12/30 18:41
 * @Version 1.0
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/settings/qx/user/login")
    public String toLoginPage() {
        return "settings/qx/user/login";
    }

    @PostMapping("/settings/qx/user/toLogin")
    @ResponseBody
    public ReturnResult toLogin(String loginAct, String loginPwd, String isRemPwd, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        //1.封装参数
        Map<String, Object> map = new HashMap<>();
        map.put("loginAct", loginAct);
        map.put("loginPwd", loginPwd);
        //2.调用service传递参数查询用户信息
        User user = userService.queryUserByLoginActAndLoginPwd(map);
        //3判断用户信息
        //3.1用户不存在，返回数据

        if (user == null) {
            System.out.println(ReturnResult.error("登陆失败，用户名或密码错误"));
            return ReturnResult.error("登陆失败，用户名或密码错误");
        }
        //3.2用户存在进一步判断
        if (!user.getAllowIps().contains(request.getRemoteAddr())) {//3.2.1 判断ip
            return ReturnResult.error("ip受限制，请更换网络");
        }
        if ((Constants.RETURN_RESULT_CODE_FAIL).equals(user.getLockState())) {//3.2.1 判断状态
            return ReturnResult.error("账号被锁定请联系管理员");
        }
        //3.2.3判断账户是否过期

        String nowDateStr = DateUtils.DateTimeFormat(new Date());
        if (nowDateStr.compareTo(user.getExpireTime()) > 0) {//过期
            return ReturnResult.error("账号已过期，请联系管理员");
        }
        session.setAttribute("sessionUser", user);
        Cookie actCookie = new Cookie("loginAct", loginAct);
        Cookie pwdCookie = new Cookie("loginPwd", loginPwd);
        if ("true".equals(isRemPwd)) {
            actCookie.setMaxAge(60 * 60 * 24 * 10);
            pwdCookie.setMaxAge(60 * 60 * 24 * 10);
        } else {
            actCookie.setMaxAge(0);
            pwdCookie.setMaxAge(0);
        }
        response.addCookie(actCookie);
        response.addCookie(pwdCookie);


        //4.登陆成功
        return ReturnResult.success(null);
    }

    @RequestMapping(value = "/settings/qx/user/logout")
    public String logout(HttpServletResponse response, HttpSession session) {
        session.removeAttribute("sessionUser");
        //session.invalidate(); 销毁session
        //清空cookie
        Cookie actCookie = new Cookie("loginAct", null);
        Cookie pwdCookie = new Cookie("loginPwd", null);
        response.addCookie(actCookie);
        response.addCookie(pwdCookie);
        return "redirect:/";
    }

}
